package net.wsfcp.android.ui.view.my.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.OrderAdapter;
import net.wsfcp.android.base.BaseFragment;
import net.wsfcp.android.bean.resultbean.OrderBean;
import net.wsfcp.android.http.mvp.contract.OrderListContract;
import net.wsfcp.android.event.ReloadOrderEvent;
import net.wsfcp.android.http.mvp.presenter.OrderListPresenter;
import net.wsfcp.android.ui.view.my.activity.OrderDetailActivity;
import net.wsfcp.android.ui.widget.RecycleViewDivider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

//订单列表
public class OrderFragment extends BaseFragment<OrderListPresenter> implements OrderListContract.OrderListView {
    @BindView(R.id.rv_order)
    XRecyclerView rvOrder;//带上下拉刷新的recycler

    OrderAdapter orderAdapter;
    ArrayList<OrderBean.OrderList> orderBeans = new ArrayList<OrderBean.OrderList>();


    int type;//订单种类  -1:已取消 0:未处理 1:配送中 2:已完成 3:已评价
    int nowPage = 1;//当前页数
    int pageSize = 20;//每页加载个数

    public static OrderFragment newInstance(int type) {
        OrderFragment orderFragment = new OrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        //fragment保存参数，传入一个Bundle对象
        orderFragment.setArguments(bundle);
        return orderFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected OrderListPresenter loadPresenter() {
        return new OrderListPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getOrderList(nowPage, pageSize, type);
    }

    @Override
    protected void initListener() {
        rvOrder.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                nowPage = 1;
                mPresenter.getOrderList(nowPage, pageSize, type);
            }

            @Override
            public void onLoadMore() {
                nowPage++;
                mPresenter.getOrderList(nowPage, pageSize, type);

            }
        });

        orderAdapter.setOnItemClickListener(new OrderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, final int position) {

                switch (view.getId()) {
                    case R.id.txt_cancel:


                        new AlertDialog.Builder(getContext())
                                .setMessage(getResources().getString(R.string.order_hint_cancel)).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();

                                mPresenter.cancelOrder(orderBeans.get(position).getId() + "");

                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                dialogInterface.dismiss();
                            }
                        }).create().show();
                        break;
                    case R.id.txt_confirm:

                        new AlertDialog.Builder(getContext())
                                .setMessage(getResources().getString(R.string.order_hint_confirm)).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();

                                mPresenter.confirmReceipt(orderBeans.get(position).getId() + "");

                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                dialogInterface.dismiss();
                            }
                        }).create().show();
                        break;


                    default:
                        Intent intent = new Intent(getContext(), OrderDetailActivity.class);
                        intent.putExtra("id", orderBeans.get(position).getId());
                        startActivity(intent);
                        break;


                }


            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });


    }

    @Override
    protected void initView() {

        if (getArguments() != null) {
            //取出保存的订单类型
            type = getArguments().getInt("type");
        }


        initAdapter();
        initRecyclerView();

    }

    public void initAdapter() {
        orderAdapter = new OrderAdapter(getContext());
    }


    public void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
//设置布局管理器
        rvOrder.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);

        //设置分隔线
        rvOrder.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.VERTICAL, dp(8), getResources().getColor(R.color.theme_grey)));
////设置增加或删除条目的动画
//        rvDrawer.setItemAnimator( new DefaultItemAnimator());


        //设置Adapter
        rvOrder.setAdapter(orderAdapter);
        rvOrder.setPullRefreshEnabled(false);//禁止下拉刷新

        rvOrder.setFootViewText("加载中...", "已获取所有订单");
    }


    @Override
    protected void otherViewClick(View view) {

    }


    @Override
    public void getOrderListSuccess(OrderBean orderBean) {
        rvOrder.refreshComplete();
        rvOrder.loadMoreComplete();

        if (nowPage == 1) {
            orderBeans.clear();
        }

        orderBeans.addAll(orderBean.getData_list());
        orderAdapter.setOrderBean(orderBeans);
        orderAdapter.notifyDataSetChanged();

        if (orderBean.getData_list().size() < pageSize) {
            rvOrder.setNoMore(true);//没有更多了

        }


    }

    @Override
    public void getOrderListFail(String failMsg) {
        rvOrder.refreshComplete();
        rvOrder.loadMoreComplete();
        rvOrder.setNoMore(true);
        orderAdapter.notifyDataSetChanged();

    }

    @Override
    public void cancelOrderSuccess() {
        toast("已取消订单");
        EventBus.getDefault().post(new ReloadOrderEvent());

    }

    @Override
    public void cancelOrderFail(String failMsg) {
        toast(failMsg);
    }

    @Override
    public void confirmReceiptSuccess() {
        toast("已确认收货");
        EventBus.getDefault().post(new ReloadOrderEvent());
    }

    @Override
    public void confirmReceiptFail(String failMsg) {
        toast(failMsg);

    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void reloadList(ReloadOrderEvent event) {
        nowPage = 1;
        mPresenter.getOrderList(nowPage, pageSize, type);

    }


}
